<template>
	<view>
		<u-popup :show="show" @close="show=false" round="10rpx" mode="center" :closeable="false">
			<view class="popup">
				<view class="text-center u-font-32 text-bold">欢迎使用掌店易</view>
				<view class="u-m-t-40 text999">为了更好的为您服务我们将获取您的手机号码</view>
				<view class="login">
					<button class="bottom" style="background: #81D8CF;" type='primary' open-type="getPhoneNumber"
						@getphonenumber="getphonenumber">
						同意获取手机号授权
					</button>
				</view>
				<view @click="show=false" class="close">暂不授权</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getOpenId,
		getPhoneNumber,
		login
	} from '@/common/api/api'

	export default {
		data() {
			return {
				show: false,
				code: '',
				form: {
					code: '',
					openid: '',
				},
			}
		},
		methods: {
			open() {
				let that = this;
				uni.login({
					provider: 'weixin',
					success: (res) => {
						that.code = res.code;
						that.getOpenId();
					}
				})
			},
			//获取openId
			getOpenId() {
				getOpenId({
					code: this.code
				}).then(res => {
					this.form.openid = res.data.openid;
					this.wxlogin();
				})
			},
			//获取手机号
			getphonenumber(e) {
				console.log(e)
				var that = this
				// 不允许授权
				if (e.detail.errMsg !== "getPhoneNumber:ok") {
					return;
				}
				that.form.code = e.detail.code;
				getPhoneNumber(that.form).then(res => {
					let isShiming = res.data.isShiming; //0 待实名 1 已实名
					that.show = false;
					that.$emit('refresh');
					// if (isShiming == 1) {
					// 	that.$emit('refresh');
					// } else {
					// 	uni.$u.route('/pages/personal/service/companybangding');
					// }
				})
			},
			//微信登录
			wxlogin() {
				login(this.form).then(res => {
					// if (res.data.isShiming == 0) {
					// 	uni.$u.route('/pages/personal/service/companybangding');
					// }
					this.$store.commit('$uStore', {
						name: '$token',
						value: res.data.openid
					});
					this.$store.commit('$uStore', {
						name: '$userInfo',
						value: res.data
					});
				});
			},
		}
	}
</script>

<style>
	.popup {
		position: relative;
		width: 600rpx;
		min-height: 250rpx;
		padding: 40rpx 40rpx 40rpx 40rpx;
	}

	.login {
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
	}

	.bottom {
		background: #81D8CF;
		width: 500rpx;
		margin-top: 50rpx;
		margin-bottom: 30rpx;
		border: none;
		font-size: 28rpx;
		border-radius: 50rpx;
	}

	.close {
		color: #999;
		font-size: 26rpx;
		text-align: center;
	}

	.center-icon {
		position: absolute;
		top: -60rpx;
		left: 50%;
		transform: translate(-50%);
		background-color: #fff;
		border-radius: 120rpx;
		overflow: hidden;
		border: 1px solid #f1f1f1;
	}
</style>